/**
 * Created by Administrator on 2016/11/10.
 */
$(function () {
    $('.findCoupons').click(function () {
        var coupons = $.trim($('.coupons').val());
        if(!isCouponid(coupons)){
            standardTips('优惠券格式不正确');
            return false;
        }
        $.post(getCouponsInfo, {coupons: coupons}, function (data) {
            console.log(data);
            var html = '';
            if (data.errno == 1) {
                var num = data.data.general_info.amount - data.data.general_info.fetched_amount;
                html += '<span><input type="radio" name="num"  class="CouNum" checked pid="'+data.data.general_info.coupon_name+'" value="'+num+'">'
                    +data.data.general_info.coupon_name + '，该券剩余<span style="color: red;font-size: 16px">'+num+'</span>张</span>';
                $('.couponInfo').html(html);
            }else{
                html+='<span>未查询到相应的优惠券</span>';
                $('.couponInfo').html(html);
            }
        }, 'json')
    })

    $('.addCoupon').click(function () {
        var mobile = $('.mobile').val().split(',');
        var unMobile = $.unique(mobile);
        var html = '';
        $.each(unMobile, function (key, val) {
            console.log(val);
            if (!isTelNumber(val)) {
                return;
            }
            html += '<span><input type="checkbox" name="mobileSub[]" class="mobileRes" value="' + val + '" checked> ' + val + '</span>&nbsp;&nbsp;';
        })
        $('.mobileInfo').html(html);
    })

    $('.formSend').submit(function (event) {
        var couponId = $('.coupons').val();
        var mobile = $('.mobile').val();
        if (mobile) {
            var len = $('.mobileInfo').find('input[type=checkbox]').length;
            if (!len) {
                standardTips('请点击添加，确认手机号');
                return false;
            }
            var chkLen = $('.mobileInfo').find('input[type=checkbox]:checked').length;
            console.log(chkLen);
            if(!chkLen){
                standardTips('请选择手机号');
                return false;
            }
        }
        var mobileFile = $('.mobileFile').val();
        if(!mobile&&!mobileFile){
            standardTips('手机号不能为空');
            return false;
        }

        if(mobile&&mobileFile){
           standardTips('请选择一种添加方式');
           return false;
        }

        console.log(couponId);
        if (couponId == '') {
            standardTips('请填写优惠券');
            return false;
        }

        if(!isCouponid(couponId)){
            standardTips('优惠券格式不正确');
            return false;
        }

        var couLen = $('.couponInfo').find('input[type=radio]').length;
        if(!couLen){
            standardTips('请点击查询优惠券');
            return false;
        }
        var  num = $('.CouNum').val();
        if(num<=0){
            standardTips('优惠券数量不足');
            return false;
        }

        $('#checkSubmit').attr('disable', true)
            .removeClass('checkSubmit')
            .removeClass('btn-primary')
            .attr('type', '');
    });
})

/**
 *
 * @param mobile
 * @returns {boolean}
 */
function isTelNumber(mobile) {
    var mobileInfo = $.trim(mobile);
    var myReg = /^1[0-9]{10}$/;
    if (mobileInfo.length != 11 || !myReg.test(mobileInfo)) {
        return false;
    }
    return true;
}

//验证优惠券
function isCouponid(id){
    var couId = $.trim(id);
    var reg = /^[0-9]*$/;
    if(reg.test(couId)){
        return true;
    }else{
        return false;
    }
}